﻿package org.yoao.crm.tools;

import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/****
 * XML配置文件处理工具类
 * @author 紫伟宸
 * @time 2009-8-25  下午02:23:39
 */
public class XMLConfig {
	
	private static final Logger log = Logger.getLogger(XMLConfig.class);
	private Document document = null;
	private String fileName = null;
	
	public XMLConfig(String fileName){
		this.fileName = fileName;
	}
	
	public boolean exists(){
		if(fileName == null || fileName.trim().equals("")) return false;
		try{
			File xmlFile = new File(fileName);
			if(xmlFile.exists()){
				return setDocument();
			}
			else{
				log.info(fileName+"配置不存在!");
				return false;
			}
		}
		catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	/****
	 * @description 得到Document
	 * @param fileName
	 * @return
	 */
	public boolean setDocument(){		
		try{
			SAXReader reader = new SAXReader();
	        document = reader.read(new File(fileName));
	        if(document != null)return true;
	        else return false;
	    }
		catch(Exception e){
	    	e.printStackTrace();
	    	return false;
		}		
	}
		
	/***
	 * @author yxwonline
	 * @description 取出指定属性的所有值 格式: /database/table/field/@name
	 * @param document
	 * @param attributeName
	 * @return List
	 */
	public List getAttributesByName(String attributeName){
		List list = document.selectNodes(attributeName);
		Iterator iter = list.iterator();
		List returnList = new ArrayList();
		while(iter.hasNext()){
			Attribute attribute = (Attribute)iter.next();
			String attributeValue = attribute.getValue();
			returnList.add(attributeValue);
		}
		return returnList;
	}
	/***
	 * @author yxwonline
	 * @description 取出指定属性的一个值 格式: /database/table/field/@name
	 * @param document
	 * @param attributeName
	 * @return String
	 */
	public String getAttributeByName(String attributeName){
		if(document == null)
			return null;
		List list = document.selectNodes(attributeName);
		Iterator iter = list.iterator();
		String attributeValue = null;
		while(iter.hasNext()){
			Attribute attribute = (Attribute)iter.next();
			attributeValue = attribute.getValue();
		}
		return attributeValue;
	}	
	
	/***
	 * @author yxwonline
	 * @description 获取某个节点的属性总个数
	 * @param document
	 * @param attributePath
	 * @return
	 */
	public int getAttributeCount(String attributePath){
		if(document == null || attributePath == null || attributePath.length()<1)
			return 0;
		List list = document.selectNodes(attributePath);
		Iterator iter = list.iterator();
		while(iter.hasNext()){
			Element element = (Element)iter.next();
			return element.attributeCount();			
		}
		return 0;
	}
	
	/***
	 * @author yxwonline
	 * @description 获取指定节点中指定属性的值
	 * @param document
	 * @param nodePath
	 * @param attributeArray
	 * @return List（String[]）
	 */
	public List getNodeValue(String nodePath, String[] attributeArray){
		if(document == null || nodePath == null || nodePath.length()<1) return null;
		List list = document.selectNodes(nodePath);
		Iterator iter = list.iterator();
		List result = new ArrayList();
		while(iter.hasNext()){
			Element element = (Element)iter.next();
			String[] values = new String[attributeArray.length];
			for(int i=0;i<attributeArray.length;i++){
				Attribute attribute = element.attribute(attributeArray[i]);
				if(attributeArray[i].equals("id") && attribute == null)
					values[i] = "1";
				else
					values[i] = attribute.getValue();
			}
			result.add(values);
		}
		return result;
	}	
	
	public String[] getC3P0Cofig(){
		if(document == null) return null;
		List list = document.selectNodes("/c3p0-config/default-config");
		Iterator iter = list.iterator();
		String[] param = new String[8];
		while(iter.hasNext()){
			Element element = (Element)iter.next();
			param[0] = this.getValue(element, "driverclass");
			param[1] = this.getValue(element, "jdbcurl");
			param[2] = this.getValue(element, "username");
			param[3] = this.getValue(element, "password");
			param[4] = this.getValue(element, "maxpoolsize");
			param[5] = this.getValue(element, "minpoolsize");
			param[6] = this.getValue(element, "maxstatements");
			param[7] = this.getValue(element, "maxidletime");
		}
		iter = null;
		list = null;
		document = null;
		return param;
	}
	
	public String getValue(Element element, String nodeName){
		Iterator iterchild = element.elementIterator(nodeName);
		while(iterchild.hasNext()){
			Element childElement=(Element)iterchild.next();
			return childElement.getText();
		}
		return null;
	}
}
